/**
 * 
 */
package com.neal.service.impl;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.neal.domain.Book;
import com.neal.domain.BookCriteria;
import com.neal.service.IBookService;


/**
 * @author Neal
 * @version 1.0
 * @description 
 */
@Service("bookService")
public class BookServiceBean implements IBookService{

	private Map<String, Book> books;
	
	public BookServiceBean(){
		books = new HashMap<String, Book>();
		books.put("001", new Book("001","Spring 1","Ray1",new GregorianCalendar(2013, 1, 1).getTime()));
		books.put("002", new Book("002","Spring 2","Ray2",new GregorianCalendar(2013, 2, 2).getTime()));
		books.put("003", new Book("003","Spring 3","Ray3",new GregorianCalendar(2013, 3, 3).getTime()));
	}
	
	/* (non-Javadoc)
	 * @see com.neal.service.IBookService#search(com.neal.domain.BookCriteria)
	 */
	@Override
	public List<Book> search(BookCriteria criteria) {
		List<Book> results = new ArrayList<Book>();
		for(Book book : books.values()){
			String keyWork = criteria.getKeyword().trim();
			String author = criteria.getAuthor().trim();
			boolean keywordMatches = keyWork.length() > 0 && book.getName().contains(keyWork);
			boolean authorMatches = author.length()> 0 && book.getAuthor().contains(author);
			if(keywordMatches || authorMatches){
				results.add(book);
			}
		}
		return results;
	}

	/* (non-Javadoc)
	 * @see com.neal.service.IBookService#findByIsbn(java.lang.String)
	 */
	@Override
	public Book findByIsbn(String isbn) {
		return books.get(isbn);
	}

	@Override
	public void showBook(Book book) {
		System.out.println(book.getIsbn());
	}

	
}
